<?php
namespace app\admin\controller;
use app\admin\model\User as UserModel;
use app\admin\model\Tiku as TikuModel;
use think\facade\Request;
use app\admin\traits\Result;
use think\validate;
use think\Db;
use Mpdf\Mpdf;

class tiku extends Common
{
    
    public function index(){
        if ($this->request->isAjax()) {
            $data = [
                'key' => $this->request->get('key', '', 'trim'),
                'limit' => $this->request->get('limit', 10, 'intval'),
            ];

            $order_name = empty($this->request->get('field')) ?'id':$this->request->get('field');
            $order_type = empty($this->request->get('order')) ?'desc':$this->request->get('order');
            
            $list = TikuModel::withSearch(['title'], ['title' => $data['key']])->order($order_name.' '.$order_type)//->order('id desc')
                ->paginate($data['limit'], false, ['query' => $data]);
               // dump(TikuModel::getlastsql());die;
            $user_date = [];
            foreach ($list as $key => $val) {
                $user_date[$key] = $val;
                //$user_date[$key]['typetitle'] = $val->typetitle;
            }
            return show($user_date, 0, '', ['count' => $list->total()]);
        }
        return $this->fetch();
        
    }

     
    
    public function edit(){
       if ($this->request->isPost()) {
            $data = $this->request->post();
			
			$validate = validate('Tiku');
			if (!$validate->check($data)) {
				//令牌数据无效时重置令牌
				$validate->getError() != '令牌数据无效' ? $token = Request::token() : $token = '';
				$msg = Result::error($validate->getError(), null, ['token' => $token]);
				return $msg;
			}
				
            if ($data['id']) {
				$data['update_time'] = time();
                //编辑
                $res = TikuModel::update($data, ['id' => $data['id']]);
                if ($res) {
                    $msg = Result::success('编辑成功', url('/admin/tiku-list'));
                } else {
                    $msg = Result::error('编辑失败');
                }
            } else {
                //添加
                $TikuModel = new TikuModel;
				$data['create_time'] = time();
                $res = $TikuModel->allowField(true)->save($data);
                if ($res) {
                    $msg = Result::success('添加成功', url('/admin/tiku-list'));
                } else {
                    $msg = Result::error('添加失败');
                }
            }
            return $msg;
        } else { 
            $id = $this->request->get('id', 0, 'intval');
            if ($id) {
                $list = TikuModel::where('id', '=', $id)->find();
                $this->assign('list', $list);
            }

            return $this->fetch();
        } 
        
    }
    
    
    public function del(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {


            $list = Db::name('shijuan')->field('title,s_id,info')->select();
            foreach ($list as $k=>$v){
                $info = json_decode($v['info'],true);
                $id_arr = [-1];
                foreach ($info as $k1=>$v2){
                    $id_arr[] = $v2['id'];
                }
                if(in_array($id,$id_arr)){
                    $msg = Result::error('试卷《'.$v['title'].'》中包含此题，请先删除');
                    return $msg;
                }
                //dump($id_arr);die;
            }


            $res = TikuModel::where('id='.$id)->delete();
                if ($res) {
                    $msg = Result::success('删除成功');
                } else {
                    $msg = Result::error('删除失败');
                }
        
                return $msg;
            
        } else {
            $this->error('参数错误');
        }
        
    }
    
    
    //清空
	public function delall()
    {
        $res = Db::name('tiku')->delete(true);
        if ($res) {
            $msg = Result::success('操作成功');
        } else {
            $msg = Result::error('操作失败');
        }
        return $msg;
    }
    
}



?>